﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ClientApplication.ComControl
{
    class CustomDgv : DataGridView
    {
        public delegate void KeyUpEventHandler(Keys keyData);
        public event KeyUpEventHandler KeyUpEvent;
        protected override bool ProcessDialogKey(Keys keyData)
        {
            Keys key = (keyData & Keys.KeyCode);
            if (key == Keys.Enter)
            {
                if (KeyUpEvent != null)
                {
                    KeyUpEvent(keyData);
                }
                //return this.ProcessRightKey(keyData);//换成后一列
                return true;//不换行，不换列
            }
            return base.ProcessDialogKey(keyData);
        }


        public new bool ProcessRightKey(Keys keyData)
        {
            Keys key = (keyData & Keys.KeyCode);
            if (key == Keys.Enter)
            {
                //第一种情况：只有一行,且当光标移到最后一列时
                if ((base.CurrentCell.ColumnIndex == (base.ColumnCount - 1)) && (base.RowCount == 1))
                {
                    base.CurrentCell = base.Rows[base.RowCount - 1].Cells[0];
                    return true;
                }
                //第二种情况：有多行，且当光标移到最后一列时,移到下一行第一个单元
                if ((base.CurrentCell.ColumnIndex == (base.ColumnCount - 1)) && (base.CurrentCell.RowIndex < (base.RowCount - 1)))
                {
                    base.CurrentCell = base.Rows[base.CurrentCell.RowIndex + 1].Cells[0];
                    return true;
                }

                return base.ProcessRightKey(keyData);
            }
            return base.ProcessRightKey(keyData);
        }

        protected override bool ProcessDataGridViewKey(KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (KeyUpEvent != null)
                {
                    KeyUpEvent(e.KeyData);
                }
                //return this.ProcessRightKey(e.KeyData);//换列
                return true;//不换行，不换列
            }
            //if (e.KeyCode == Keys.F4)
            //{
            //    return this.ProcessRightKey(e.KeyData);
            //}
            return base.ProcessDataGridViewKey(e);
        }



    }
}
